package com.dhcc.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

public class MapSignUtil {

	public static Map<String, String> sortMapStrByKey(Map<String, String> map) {
		if (map == null || map.isEmpty()) {
			return null;
		}
		Map<String, String> sortMap = new TreeMap<String, String>();
		sortMap.putAll(map);
		return sortMap;
	}
	public static String getStrData(Map<String, String> data) {
		// 参数排序
		Map<String, String> map = sortMapStrByKey(data);
		StringBuffer sb = new StringBuffer();
		if(map != null) {
			Iterator<Entry<String, String>> iter = map.entrySet().iterator();
			while (iter.hasNext()) {
				Entry<String, String> entry = iter.next();
				String key = entry.getKey();
				Object value = entry.getValue();
				if (null != value && !"".equals(value)) {
					if (!iter.hasNext()) {
						sb.append(key).append("=").append(value);
					} else {
						sb.append(key).append("=").append(value).append("&");
					}
				}
			}
			String result = sb.toString();
			return result;
		}
		return null;
	}
	
}
